/**
 *  StaDiUM - State Diagram Unified Modeler
 *  Copyright (C) 2007  Stefan Bucur, Mihai Balan, Claudiu Tanase
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 *  File Information
 *
 *  Last Modified On: $Date$
 *  Last Modified By: $Author$
 */
package ro.pub.cs.stadium.platform;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import ro.pub.cs.stadium.platform.general.OkCancelDialog;
import ro.pub.cs.stadium.plugin.IPlugin;

/**
 * @author stefan
 *
 */
public class PluginWindow extends OkCancelDialog{
	
	private final MainWindow mainWnd;
	private final Label pluginLabel;
	private final Table pluginTable;
	
	private Image defaultPlugIcon;
	
	public PluginWindow(MainWindow mainWnd) {
		super(mainWnd.getShell(), true);
		
		this.mainWnd = mainWnd;
		
		getWndShell().setText("Plugin Management");
		
		pluginTable = new Table(getWndShell(), SWT.SINGLE | SWT.FULL_SELECTION);
		
		pluginLabel = new Label(getWndShell(), SWT.NONE);
		pluginLabel.setText("The list of registered plugins:");
		
		FormData pluginData = new FormData();
		pluginData.left = new FormAttachment(0);
		pluginData.right = new FormAttachment(100);
		pluginData.bottom = new FormAttachment(getButtonSep(), -5);
		pluginData.top = new FormAttachment(pluginLabel, 5);
		
		pluginTable.setLayoutData(pluginData);
		pluginTable.setHeaderVisible(true);
		pluginTable.setLinesVisible(true);
		
		TableColumn nameCol = new TableColumn(pluginTable, SWT.LEFT);
		nameCol.setWidth(300);
		nameCol.setText("Name");
		
		TableColumn typeCol = new TableColumn(pluginTable, SWT.LEFT);
		typeCol.setWidth(300);
		typeCol.setText("Type");
		
		TableColumn statusCol = new TableColumn(pluginTable, SWT.CENTER);
		statusCol.setWidth(100);
		statusCol.setText("Status");
		

		
		FormData labelData = new FormData();
		labelData.left = new FormAttachment(0);
		labelData.right = new FormAttachment(100);
		labelData.top = new FormAttachment(0);
		
		pluginLabel.setLayoutData(labelData);
		
		try {
			InputStream imageStream = this.getClass().
				getResourceAsStream("icons/plugin16.png");
	
			defaultPlugIcon = new Image(getWndShell().getDisplay(), imageStream);
	
			imageStream.close();
		} catch (IOException ex) {
			defaultPlugIcon = null;
		}
	}

	@Override
	protected void doCancelAction() {
		modalResult = -1;
		
		getWndShell().close();
		
	}

	@Override
	protected void doOkAction() {
		modalResult = 0;
		
		getWndShell().close();
		
	}
	
	@Override
	public int showModal() {
		PluginManager plugMan = mainWnd.getPluginManager();
		
		IPlugin[] plugins = plugMan.getRegisteredPlugins();
		
		if (plugins.length > 0) {
			for (int i=0; i < plugins.length; i++) {
				TableItem newItem = new TableItem(pluginTable, SWT.NONE);
				
				newItem.setText(new String[] {
						plugMan.getPluginName(plugins[i]),
						plugMan.getPluginTypeName(plugins[i]),
						"Active"
					});
				
				newItem.setImage(defaultPlugIcon);
				
			}
		} else {
			TableItem noItem = new TableItem(pluginTable, SWT.NONE);
			noItem.setText("No Plugins Installed");
			
			
		}
		return super.showModal();
	}
}
